package com.xingkeduo.remote;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xingkeduo.dto.HairstyleResponseDTO;
import com.xingkeduo.dto.ServprodTable;
import com.xingkeduo.dto.ShopsDaysTable;
import com.xingkeduo.dto.transter.orderdetail.Hairstyle_list;
import com.xingkeduo.dto.transter.orderdetail.Selected_hair;
import com.xingkeduo.dto.transter.shop.ShopsDTO;
import com.xingkeduo.dto.transter.shop.StoreRemoteDto;
import com.xingkeduo.remote.feign.StoreFeignService;
import com.xingkeduo.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chi  2019-03-13 10:43
 **/
@Service
@Slf4j
public class StoreRemoteService {

    @Resource
    private StoreFeignService storeFeignService;

    public StoreRemoteDto getByStoreId(int storeId){
        ResponseUtils<ShopsDTO> responseUtils = storeFeignService.doGet((long) storeId);
        if(responseUtils.success()){
            ShopsDTO shopsDTO= responseUtils.getData();
            return StoreRemoteDto.builder()
                    .storeId(Math.toIntExact(shopsDTO.getId()))
                    .storeName(shopsDTO.getName())
                    .cityId(String.valueOf(shopsDTO.getCitiesId()))
                    .cityName(shopsDTO.getCitiesName())
                    .branchOfficeId(String.valueOf(shopsDTO.getOfficeId()))
                    .branchOfficeName(shopsDTO.getOfficeName())
                    .vendorId(String.valueOf(shopsDTO.getVendorId()))
                    .vendorName(shopsDTO.getVendorName())
                    .storeAddress(shopsDTO.getAddress())
                    .daysList(shopsDTO.getDaysList())
                    .build();
        }
        return null;
    }


    /**
     * 根据店铺id获取店铺信息
     * @param storeId
     * @return
     */
    public ShopsDTO queryStoreById(int storeId){
        ResponseUtils<ShopsDTO> responseUtils = storeFeignService.doGet((long) storeId);
        if(responseUtils.success()){
            ShopsDTO shopsDTO= responseUtils.getData();
            return shopsDTO;
        }
        return null;
    }


    public Selected_hair hairstyle(int urlParam){
        JSONObject object = new JSONObject();
        object.put("id",urlParam);
        Selected_hair selected_hair = new Selected_hair();
        ResponseUtils<HairstyleResponseDTO> hairstyleResponseDTO = storeFeignService.doGetListsInfo(object.toString());
        selected_hair.covert(hairstyleResponseDTO.getData());
        return selected_hair;
    }


    public List<Hairstyle_list> queryHairStyle(){
        JSONObject object = new JSONObject();
        object.put("id","");
        ResponseUtils<List<HairstyleResponseDTO>> responseUtils= storeFeignService.doGetLists(object.toString());
        log.info("queryHairStyle:{}", JSON.toJSONString(responseUtils));
        if(responseUtils.success() && responseUtils.getData()!=null){
            List<Hairstyle_list> lists  = responseUtils.getData().stream().limit(5)
                    .map(this::queryCovertToDto)
                    .collect(Collectors.toList());
            return lists;
        }
        return new ArrayList<>();
    }


    public Hairstyle_list queryCovertToDto(HairstyleResponseDTO hairstyleResponseDTO){
        if(hairstyleResponseDTO==null){
            return null;
        }

        Hairstyle_list hairstyle_list = new Hairstyle_list();
        hairstyle_list.covert(hairstyleResponseDTO);
        return hairstyle_list;

    }


    /**
     * 获取店铺营业时间
     * @param shopsId
     * @return
     */
    public ShopsDaysTable doGetShopServprodDetail(String shopsId){
        ResponseUtils<List<ShopsDaysTable>> responseUtils = storeFeignService.doGetShopServprodDetail(Long.valueOf(shopsId));
        if(responseUtils.success()){
            List<ShopsDaysTable> list = responseUtils.getData();
            if(list!=null && list.size()>0){
                return list.get(0);
            }
        }
        return null;
    }


    /**
     * 获取服务名称
     * @param id
     * @return
     */
    public String getServprod(String id){
        ResponseUtils<ServprodTable>  responseUtils = storeFeignService.getServprod(Long.valueOf(id));
        if(responseUtils.success()){
            return responseUtils.getData().getName();
        }
        return null;
    }
}
